var WxParse = require('../../wxParse/wxParse.js');
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    lunbo1:'',
    lunbo2: '',
    lunbo3: '',
    is_like: false,
    tab_key: "pro_des", // 详情：pro_des，属性：pro_nature
    num: 1,
    // 使用data数据对象设置样式名  
    minusStatus: 'disabled',
    is_show_modal:false,
    productClassify:'',
    productName:'',
  
    productPrice:"",
    productWeight:"",
    pro_id:""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    console.log(options)
    let id = options.proId
    var that = this

    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/product/proDetails?proId=' + id + '&openid=' + openid,
          success: function (res) {
            console.log("proDetails",res)
            wx.hideLoading()
            console.log(res.data.isCollection)
            var img1 = res.data.qhcPro.productPicture
            var img2 = res.data.qhcPro.productPicTwo
            var img3 = res.data.qhcPro.productPicPhree;
            //轮播图
            WxParse.wxParse('rich_img1', 'html', img1, that, 0);
            WxParse.wxParse('rich_img2', 'html', img2, that, 0);
            WxParse.wxParse('rich_img3', 'html', img3, that, 0);
            //商品详情图
            var img_detail = res.data.qhcPro.productDetailsPic
            WxParse.wxParse('img_detail', 'html', img_detail, that, 0);
            //商品图
            var pro_img = res.data.qhcPro.productClassPic
            WxParse.wxParse('pro_img', 'html', pro_img, that, 0);

            that.setData({
              pro_id: res.data.qhcPro.id,
              productName: res.data.qhcPro.productName,
              discount: res.data.qhcPro.discount,
              productPrice: res.data.qhcPro.productPrice,
              productWeight: res.data.qhcPro.productWeight,
              is_like: res.data.isCollection,
            })
          }
        })
      },
    })    
    
  },
  skipShoppingCart: function () {
    wx.navigateTo({
      url: '../shoppingCart/shoppingCart'
    })
  },
  skipMy:function(){
    wx.switchTab({
      url: '../my/my'
    })
  },
  showModal:function(){
    this.setData({
      is_show_modal:true
    })
  },
  closeModal:function(){
    this.setData({
      is_show_modal: false
    })
  },
  changeDes: function() {
    this.setData({
      tab_key: "pro_des"
    })
  },
  changeNature: function() {
    this.setData({
      tab_key: "pro_nature"
    })
  },
  collect: function() {
    // this.setData({
    //   is_like: !this.data.is_like
    // })
    var that = this
    if (that.data.is_like == false) {
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data

          wx.request({
            url: app.globalData.URL + '/app/collection/collect?productId=' + that.data.pro_id + '&openid=' + openid,
            success: function (res) {
              console.log(res)
              if (res.data.msg == true) {
                that.setData({
                  is_like: true
                })
                wx.showToast({
                  title: '收藏成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            }
          })
        },
      })
      
      
    } else {
      wx.getStorage({
        key: 'openid',
        success: function(res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/collection/cancelCollect?productId=' + that.data.pro_id + '&openid=' + openid,
            success: function (res) {
              console.log(res)
              if (res.data.msg == true) {
                that.setData({
                  is_like: false
                })
                wx.showToast({
                  title: '取消成功',
                  icon: 'success',
                  duration: 2000
                })
              }
            }
          })
        },
      })
      
    }

  },
  bindMinus: function() {
    var num = this.data.num;
    // 如果大于1时，才可以减  
    if (num > 1) {
      num--;
    }
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num <= 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus,      
    });
  },
  /* 点击加号 */
  bindPlus: function() {
    var num = this.data.num;
    // 不作过多考虑自增1  
    num++;
    // 只有大于一件的时候，才能normal状态，否则disable状态  
    var minusStatus = num < 1 ? 'disabled' : 'normal';
    // 将数值与状态写回  
    this.setData({
      num: num,
      minusStatus: minusStatus
    });
  },
  /* 输入框事件 */
  bindManual: function(e) {
    var num = e.detail.value;
    // 将数值与状态写回  
    this.setData({
      num: num
    });
  },
  addShopping:function(){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/product/addShopping?proId=' + that.data.pro_id + '&num=' + that.data.num + '&openid=' + openid,
          success: function (res) {
            console.log(res)
            if (res.data.msg == true) {
              wx.showToast({
                title: '加入成功',
                icon: 'success',
                duration: 5000
              })
            } else {
              wx.showToast({
                title: '加入购物车失败',
                icon: '../../images/error.png',
                duration: 5000
              })
            }
          }
        })
      },
    })
    
  },

  toPay:function(){
    // if(this.data.discount){
    //   wx.navigateTo({
    //     url: '../shoppingList/shoppingList?ids=' + this.data.pro_id + '&number=' + this.data.num + '&money=' + this.data.discount * this.data.num + '&isPro=1',
    //   })
    // }else{
    //   wx.navigateTo({
    //     url: '../shoppingList/shoppingList?ids=' + this.data.pro_id + '&number=' + this.data.num + '&money=' + this.data.productPrice * this.data.num + '&isPro=1',
    //   })
    // }
    wx.navigateTo({
      url: '../shoppingList/shoppingList?ids=' + this.data.pro_id + '&number=' + this.data.num + '&money=' + this.data.discount * this.data.num + '&isPro=1',
    })    
  }
  
})